package down{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.OutputProgressEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import flash.utils.getTimer;
	import ui.down.DownloadingUI;
	import utils.PNGEncoder;
	/**
	 * ...
	 * @author SXL
	 */
	public class TestSocket extends DownloadingUI{
		private var timer:int;
		private var socket:Socket;
		private var isFirst:Boolean;
		private var total:int;
		private var loaded:int;
		private var isClose:Boolean;
		private var sendByte:ByteArray;
		public function TestSocket(){
			Main.traces("testSocket");
			socket = new Socket();
			
			socket.endian=Endian.LITTLE_ENDIAN;
			socket.addEventListener(Event.CLOSE,onClose);
			socket.addEventListener(Event.CONNECT,onConnect);
			socket.addEventListener(StatusEvent.STATUS,onClose);
			socket.addEventListener(IOErrorEvent.IO_ERROR,onClose);
			socket.addEventListener(ProgressEvent.SOCKET_DATA,onProgress);
			socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onClose);
			socket.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS,onOutputProgress);
			Main.traces("Toconnect");
			socket.connect("220.181.85.226",21356);
			this.isFirst=true;
			App.scene.scene=this;
		}
		private function onOutputProgress(evt:OutputProgressEvent):void{
			//trace("onOutputProgress  "+evt.bytesPending+"   "+(getTimer()-this.timer));
		}
		private function send():void{
			socket.writeBytes(this.sendByte);
			socket.flush();	
			
		}
		private function sendFrist():void{
			socket.writeUTFBytes("XBWEBNETSPEEDCHECK");
			socket.flush();	
		}
		private function onProgress(evt:ProgressEvent):void{
			if(socket.bytesAvailable < 4) return;
			var length:int = socket.readUnsignedInt();
			this.loaded+=length;
			Main.traces("onProgress "+length+"   "+(getTimer()-this.timer));
			this.timer=getTimer();
			this.doNextSend()
		}
		private function doNextSend():void{
			Main.traces("send");
			this.timer=getTimer();
			this.send();
		}
		private function onConnect(evt:Event):void{
			Main.traces("onConnect");
			
			var bmd:BitmapData=App.asset.getBitmapData("png.down.godBg");
			var bigBmd:BitmapData=new BitmapData(30,30);
			bigBmd.copyPixels(bmd,new Rectangle(0,0,30,30),new Point(0,0));
			this.sendByte=PNGEncoder.encode(bigBmd);
			
			this.sendFrist();
			this.isClose=false;
		}
		private function onClose(evt:Event):void{
			Main.traces("close  "+evt.type+"  "+JSON.stringify(evt));
		}
		public static function checkByteArray(bytes:ByteArray):void{
			bytes.position=0;
			var dStr:String="";
			var value:uint;
			for(var i:int=0;i<bytes.length;i++){
				value=uint(bytes.readUnsignedByte());
				dStr+="0x"+value.toString(16)+"    ";
			}
			trace(dStr);
			bytes.position=0;
		}
		public static function byteArrayToString(bytes:ByteArray):String{
			var str:String="";
			bytes.position=0;
			for(var i:int=0;i<bytes.length;i++){
				str+=(String.fromCharCode(bytes.readByte().toString()));
			}
			bytes.position=0;
			trace(str);
			return str;
		}
	}
}